package com.util.sql;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Connection;
import javax.sql.DataSource;

import com.util.configuration.ConfigurationParametersManager;

/**
 * A non-pooled implementation of a <code>DataSource</code>.
 * <p>
 * <b>WARNING:</b> The only method implemented of <code>DataSource</code> is
 * <code>getConnection()</code>. Rest of methods throw the unchecked exception 
 * <code>UnsupportedOperationException</code>.
 * <p>
 * Required configuration properties:
 * <ul>
 * <li><code>SimpleDataSource/driverClassName</code>: it must specify the full
 * class name of the JBDC driver.</li>
 * <li><code>SimpleDataSource/url</code>: the database URL (without user and
 * password).</li>
 * <li><code>SimpleDataSource/user</code>: the database user.</li>
 * <li><code>SimpleDataSource/password</code>: the user's password.</li>
 * </ul>
 */
public class SimpleDataSource implements DataSource {

    private static final String DRIVER_CLASS_NAME_PARAMETER = 
        "SimpleDataSource/driverClassName";
    private static final String URL_PARAMETER = 
        "SimpleDataSource/url";
    private static final String USER_PARAMETER = 
        "SimpleDataSource/user";
    private static final String PASSWORD_PARAMETER = 
        "SimpleDataSource/password";
        
    private static String url;
    private static String user;
    private static String password;
        
    static {

        try {
    
            /* Read configuration parameters. */
            String driverClassName = 
                ConfigurationParametersManager.getParameter(
                    DRIVER_CLASS_NAME_PARAMETER);
            url = ConfigurationParametersManager.getParameter(URL_PARAMETER);
            user = ConfigurationParametersManager.getParameter(USER_PARAMETER);
            password = ConfigurationParametersManager.getParameter(
                PASSWORD_PARAMETER);
//            String driverClassName = "org.postgresql.Driver";
//            url = "jdbc:postgresql://172.20.50.2:5432/djmobile";
//            user = "movistarplaya";
//            password = "movistarplaya";

            /* Load driver. */    
            Class.forName(driverClassName);
        
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }
        
    public Connection getConnection(String username, String password)
        throws SQLException {
        
        throw new UnsupportedOperationException("Not implemented");
        
    }

    public PrintWriter getLogWriter() throws SQLException {    
        throw new UnsupportedOperationException("Not implemented");    
    }

    public void setLogWriter(PrintWriter out) throws SQLException {
        throw new UnsupportedOperationException("Not implemented");
    }

    public void setLoginTimeout(int seconds) throws SQLException {
        throw new UnsupportedOperationException("Not implemented");
    }

    public int getLoginTimeout() throws SQLException {
        throw new UnsupportedOperationException("Not implemented");
    }

    /* Test code. Uncomment for testing. */
//    public static void main (String[] args) {
//        
//        try {
//        
//            /* Trying to get a connection. */
//            System.out.println("Trying to get a connection");
//            DataSource dataSource = new SimpleDataSource();
//            Connection connection = dataSource.getConnection();
//            
//            /* Close connection. */
//            connection.close();
//            
//            /* Test OK. */
//            System.out.println("Test OK");            
//            
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        
//    }

}
